package com.example.ribbon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.auth.domain.DataNode;
import com.example.config.ribbon.ServiceCaller;
import com.example.feign.UserFeignService;
import com.example.pay.domain.UserMoneyAccount;
import com.example.ribbon.PayConstant;
import com.example.ribbon.service.PayAccountRibbonService;
import com.example.util.constant.NodeUrlConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class PayAccountRibbonServiceImpl implements PayAccountRibbonService {

    @Autowired
    @Lazy
    ServiceCaller serviceCaller;

    @Autowired
    UserFeignService userFeignService;

    @Override
    public JSONObject getUserMoneyAccountInfo(String userName) {
        DataNode dataNode=userFeignService.getUserNodeByNodeNo(userName, PayConstant.SERVER_NAME);
        String serverName=dataNode.getNodeServerName();
        return serviceCaller.get(serverName,PayConstant.USERMONEY_INFO_URL, JSONObject.class);
    }

    @Override
    public JSONObject insertMoney(String userName, String money) {
        DataNode dataNode=userFeignService.getUserNodeByNodeNo(userName, PayConstant.SERVER_NAME);
        String serverName=dataNode.getNodeServerName();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("money",money);
        jsonObject.put("userName",userName);
        return serviceCaller.post(serverName,PayConstant.USERMONEY_INSVERTMONEY_URL,jsonObject,JSONObject.class);
    }

    @Override
    public JSONObject transfer(String fromUid, String toUserName, String payPassWord, String money, String note) {
        DataNode dataNode=userFeignService.getUserNodeByNodeNoUserId(fromUid, PayConstant.SERVER_NAME);
        String serverName=dataNode.getNodeServerName();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("userName",toUserName);
        jsonObject.put("money",money);
        jsonObject.put("payPassword",payPassWord);
        jsonObject.put("payNote",note);
        return serviceCaller.post(serverName,PayConstant.USERMONEY_ACCOUNTS_TRANSFER_URL,jsonObject,JSONObject.class);
    }


    @Override
    public JSONObject transferRecevie(String uid, String fromUid, String money, String note) {
        DataNode dataNode=userFeignService.getUserNodeByNodeNoUserId(uid, PayConstant.SERVER_NAME);
        String serverName=dataNode.getNodeServerName();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("userId",uid);
        jsonObject.put("money",money);
        jsonObject.put("fromUid",fromUid);
        jsonObject.put("payNote",note);
        return serviceCaller.post(serverName,PayConstant.USERMONEY_ACCOUNTS_RECEVIE_URL,jsonObject,JSONObject.class);
    }

    @Override
    public JSONObject getPayRecordOutPage(String userId, Integer pageNum, Integer pageSize) {
        DataNode dataNode=userFeignService.getUserNodeByNodeNoUserId(userId, PayConstant.SERVER_NAME);
        String serverName=dataNode.getNodeServerName();
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("pageNum",pageNum.toString());
        params.add("pageSize",pageSize.toString());
        String url = UriComponentsBuilder
                .fromUriString(PayConstant.USERMONEY_OUT_URL)
                .queryParams(params)
                .toUriString();
        return serviceCaller.get(serverName,url,JSONObject.class,new JSONObject());
    }

    @Override
    public JSONObject getPayRecordInPage(String userId, Integer pageNum, Integer pageSize) {
        DataNode dataNode=userFeignService.getUserNodeByNodeNoUserId(userId, PayConstant.SERVER_NAME);
        String serverName=dataNode.getNodeServerName();
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("pageNum",pageNum.toString());
        params.add("pageSize",pageSize.toString());
        String url = UriComponentsBuilder
                .fromUriString(PayConstant.USERMONEY_IN_URL)
                .queryParams(params)
                .toUriString();
        return serviceCaller.get(serverName,url,JSONObject.class,new JSONObject());
    }


}
